<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="key" label="键" width="240">
			</el-table-column>
			<el-table-column prop="value" label="值" width="240">
			</el-table-column>
      <el-table-column prop="intro" label="描述" width="200">
      </el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :page-size="query.pageSize"
          :total="pageInfo.total"
          :current-page="query.currentPage"
          style="float:right;">
			</el-pagination>
		</el-col>

		<!--
		新增/修改界面
		-->
		<el-dialog title="新增/修改" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="键">
					<el-input v-model="saveForm.key" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="值">
          <el-input v-model="saveForm.value" auto-complete="off"></el-input>
        </el-form-item>
				<el-form-item label="描述">
					<el-input type="textarea" v-model="saveForm.intro"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="save" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				Configs: [],
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				saveFormVisible: false,//新增界面是否显示
				addLoading: false,
				saveFormRules: {
					name: [
						{ required: true, message: '请输入名字', trigger: 'blur' }
					]
				},
				//新增/修改-界面数据
				saveForm: {
				  id:null,
					key: '',
					value: '',
          intro:""
				},
        query:{
				  currentPage:1,  // 当前页
          pageSize:10, // 每页显示条数
          keyword:""  // 关键字-查询条件
        },
        pageInfo:{
				  total:0,  // 总条数
          rows:[] // 当前页数据
        },
        employees:[],  // 所有的员工  两种方案:一进入页面就去查询并赋值  二是点击新增/修改后,查询并赋值(采纳这种)
        deptTree:[] // 父部门的部门树
			}
		},
		methods: {
      search(){
        // 将当前页设置为1
        this.query.currentPage = 1;
        this.getConfigs();
      },
			handleCurrentChange(val) {  // 当 当前页的值被改变时触发
				// 将val(当前页)赋值给query.currentPage
        this.query.currentPage = val;
        // 调用查询方法,重新查询
				this.getConfigs();
			},
			//获取部门列表
			getConfigs() {
        console.log(this.query)
        this.$http.post("/config",this.query)
            .then(result => { // 后端操作成功,进入then()
              console.log(result);
              result = result.data; // {success:true,message:"",dataObj:{}}
              if(result.success){ // true 后端返回了数据
                this.pageInfo = result.dataObj;
              }else{ // false 说明后端处理失败
                this.$message({message: result.message,type: 'error'});
              }
            })
            .catch(result => {  // 如果报错 就会进入catch()
              this.$message({message: "网络异常,请检查网络!",type: 'error'});
            })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除这个数据吗?', '提示', {
					type: 'warning'
				}).then(() => {
					// 1 获取当前要删除数据的ID
          let id = row.id;
          // 2 调后端的删除接口
          this.$http.delete("/config/"+id)
                    .then(result => {
                        result = result.data; // {success:true,message:""}
                        if(result.success){
                          // 提示"删除成功"
                          this.$message({message: "删除成功!",type: 'success'});
                          // 删除成功以后,要重新查询数据,并且要从第一页开始进行查询
                          this.query.currentPage = 1;
                          // 调用查询方法
                          this.getConfigs();
                          // this.search();
                        }else{
                          this.$message({message: result.message,type: 'error'});
                        }
                    })
                    .catch(result => {
                      this.$message({message: "网络异常,请检查网络!",type: 'error'});
                    })
				}).catch(() => {
          this.$message({message: '已取消删除!',type: 'info'});
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        // 展示弹框
				this.saveFormVisible = true;
				// 将当前行的对象赋值给this.saveForm对象
				this.saveForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
        // 显示弹框
				this.saveFormVisible = true;
				// 初始化弹框的值,为了防止将上次的内容带到了下一次
				this.saveForm = {
          id:null,
          key: '',
          value: '',
          intro:""
        }
			},
			//新增/修改的保存
			save: function () {
        // this.$refs.saveForm.validate((valid) => { 校验表单是否满足校验规则 valid=true 满足校验规则
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true; // 加载框
                // 要处理传参
                let param = this.saveForm;
                // 调用后端接口-axios
                this.$http.put("/config",param)
                    .then(result => {
                        // 获取后端返回的结果
                        result = result.data; // {success:true,message:""}
                        if(result.success){
                          this.$message({message: "保存成功!",type: 'success'});
                          // 关闭弹框
                          this.saveFormVisible = false;

                          // 关闭加载框
                          this.addLoading = false;

                          // 重新查询数据-(从当前页查询还是回到第一页,看需求)
                          this.query.currentPage = 1;
                          this.getConfigs();
                        }else{
                          this.$message({message: result.message,type: 'error'});
                        }
                    })
                    .catch(result => {
                      this.$message({message: "网络异常,请检查网络!",type: 'error'});
                    })
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
        // 1 获取选中的要删除行的ID
				var ids = this.sels.map(item => item.id);
        console.log(ids)
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
          // 2 调后端的删除接口
          this.$http.patch("/config",ids)
              .then(result => {
                result = result.data; // {success:true,message:""}
                if(result.success){
                  // 提示"删除成功"
                  this.$message({message: "删除成功!",type: 'success'});
                  // 删除成功以后,要重新查询数据,并且要从第一页开始进行查询
                  this.query.currentPage = 1;
                  // 调用查询方法
                  this.getConfigs();
                  // this.search();
                }else{
                  this.$message({message: result.message,type: 'error'});
                }
              })
              .catch(result => {
                this.$message({message: "网络异常,请检查网络!",type: 'error'});
              })
				}).catch(() => {
          this.$message({message: '已取消删除!',type: 'info'});
				});
			}
		},
		mounted() {
		  // 挂载完毕,就要去后端查询分页数据并展示
			this.getConfigs();
		}
	}

</script>

<style scoped>

</style>